/**
 * @author casazaft
 */
Package("xclab.mvc.core").Class("View", {
        View: function(){
 this.Super.constructor();            this.mediatorMap = {};
            this.observerMap = {};
            this.initializeView();
        },
        Methods: {
            initializeView: function(){
            
            },
            registerObserver: function(noteName, observer){
                var observers = this.observerMap[noteName];
                
                if (observers) {
                    observers.push(observer);
                }
                else {
                    this.observerMap[noteName] = [observer];
                    
                }
            },
            notifyObservers: function(note){
            
                if (this.observerMap[note.getName()]) {
                
                    var observersRef = this.observerMap[note.getName()];
                    
                    var observers = [];
                    var observer = null;
                    for (var i = 0; i < observersRef.length; i++) {
                        observer = observersRef[i];
                        observers.push(observer);
                    }
                    
                    for (var i = 0; i < observers.length; i++) {
                        observer = observers[i];
                        observer.notifyObserver(note);
                    }
                }
            },
            removeObserver: function(noteName, noteContext){
                var observers = this.observerMap[noteName];
                var observeClass = null;
                for (var i = 0; i < observers.length; i++) {
                    observeClass = observers[i];
                    if (observeClass.compareNotifyContext(noteContext) == true) {
                        observers.splice(i, 1);
                        break;
                        
                        
                    }
                }
                if (observers.length == 0) {
                    delete this.observerMap[noteName];
                }
            },
            registerMediator: function(mediator){
                var Observer = Import("xclab.mvc.patterns.observer.Observer");
                if (this.mediatorMap[mediator.getMediatorName()]) 
                    return;
                this.mediatorMap[mediator.getMediatorName()] = mediator;
                var interests = mediator.listNotificationInterests();
                if (interests.length > 0) {
                    var observer = null;
                    for (var i = 0; i < interests.length; i++) {
                        observer = new Observer(mediator.handleNotification, mediator);
                        this.registerObserver(interests[i], observer);
                    }
                }
                mediator.onRegister();
            },
            retrieveMediator: function(mediatorName){
                return this.mediatorMap[mediatorName];
            },
            removeMediator: function(mediatorName){
                var mediator = this.mediatorMap[mediatorName];
                if (mediator) {
                    var interests = mediator.listNotificationInterests();
                    for (var i = 0; i < interests.length; i++) {
                        this.removeObserver(interests[i], mediator);
                    }
                    delete this.mediatorMap[mediatorName];
                    mediator.onRemove();
                }
                return mediator;
            },
            hasMediator: function(mediatorName){
                return this.mediatorMap.hasOwnProperty(mediatorName);
            }
            
            
        },
        Extends: j2j.patterns.Singleton
    })
